package com.wonderli.example.sensortest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SensorTest extends ListActivity implements SensorEventListener{
    /** Called when the activity is first created. */
    private SensorManager smgr;
    private Intent intent;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        smgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        List<Sensor> slist = smgr.getSensorList(Sensor.TYPE_ALL);
        List<String> slistName = new ArrayList<String>();
        ListView lv = getListView();
        intent = new Intent();
        for(Sensor sensor:slist){        	
        	slistName.add(sensor.getName());
        }        
        setListAdapter(new ArrayAdapter<String>(SensorTest.this, R.layout.list_item_view, slistName));
        lv.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
				//Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
				//        Toast.LENGTH_SHORT).show();
				//System.out.println(id);
				if(id == 0){
					intent.setClass(SensorTest.this, AccelerometerSensor.class);
					startActivity(intent);
				} else if(id == 1){
					intent.setClass(SensorTest.this, MagneticfieldSensor.class);
					startActivity(intent);
				} else if(id == 2){
					intent.setClass(SensorTest.this, OrientationSensor.class);
					startActivity(intent);
				} else if(id == 3){
					intent.setClass(SensorTest.this, ProximitySensor.class);
					startActivity(intent);
				}
				
				
			}
        	
        });
    }	
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		
	}
}